package com.jdd.modules.sdk.utils;


import com.jdd.common.constant.JddConstant;
import com.jdd.common.util.ByteUtils;
import com.jdd.common.util.CrcUtil;

import java.util.Arrays;

/**
 * author:lcy
 * since 2021/10/26 13:50
 */
public class SmallFangKongLedUtils {
    /**
     * 头信息
     */
    private static final String head = "AA55";
    /**
     * 流水号
     */
    private static final String serial = "00";
    /**
     * 地址
     */
    private static final String site = "64";
    /**
     * 业务类型
     */
    private static final String type = "00";
    /**
     * 屏幕显示
     */
    private static final String pushPlayOrder = "27";
    /**
     * 播放语音命令
     */
    private static final String playVoice = "22";
    /**
     * 结尾符
     */
    private static final String stopCode = "AF";

    /**
     * 向led屏幕发送临时播放信息
     *
     * @param str  发送的数据
     * @param line 行号  第一行01000100 第二行02000100 第三行03000100 第四行04000100
     * @return
     */
    public static byte[] ledScreenShowAgreement(String str, String line) throws Exception {
        if (str == null || str == "") {
            return null;
        }
        StringBuilder buffer = new StringBuilder();
        byte[] bytes = str.getBytes("GBK");
        String formatted = bytes2HexString(bytes);
        String a = line + formatted;
        //获取字节长度
        int length = a.length() / 2;
        //高位字节
        int mainVersion = (length & 0xFF00) >> 8;
        //低位
        int minorVersion = length & 0xFF;
        String tall = numToHex16(mainVersion);
        String low = numToHex16(minorVersion);
        //包头
        buffer.append(head);
        //流水号
        buffer.append(serial);
        //地址
        buffer.append(site);
        //业务类型
        buffer.append(type);
        //命令
        buffer.append(pushPlayOrder);
        //指定长度的高位
        buffer.append(tall);
        //指定长度的低位
        buffer.append(low);
        buffer.append(line);
        buffer.append(formatted);
        //除去头尾信息用CRC-16/MODBUS算法得到校验字符串
        byte[] bytes1 = hexToByteArray(serial + site + type + pushPlayOrder + tall + low + line + formatted + "0000");
        String crcData = getCRCData(bytes1);
        buffer.append(crcData);
        buffer.append(stopCode);

        return hexToByteArray(buffer.toString());
        //祝您健康
        //AA 55 36 64 00 27 00 0C 02 00 01 00 D7 A3 C4 FA BD A1 BF B5 A0 43 AF
        //本机
        //AA 55 2F 64 00 27 00 0C 01 00 01 00 D7 A3 C4 FA BD A1 BF B5 A8 5E AF
    }
    /**
     * LED语音播放指令
     *
     * @param str //播放语音的内容
     * @return
     * @throws Exception
     */
    public static byte[] ledPlayVoice(String str) throws Exception {
        if (str == null || str == "") {
            return null;
        }
        StringBuilder buffer = new StringBuilder();
        StringBuilder formatted = new StringBuilder();
        //根据，拆解播报类容
        String[] split = str.split(",");
        for (String spl : split) {
            byte[] bytes = spl.getBytes("GBK");
            formatted.append("".equals(nType.getTypeCode(spl).getValue()) ?  bytes2HexString(bytes): nType.getTypeCode(spl).getValue());
        }
        //获取字节长度
        int length = formatted.length() / 2;
        //高位字节
        int mainVersion = (length & 0xFF00) >> 8;
        //低位
        int minorVersion = length & 0xFF;
        String tall = numToHex16(mainVersion);
        String low = numToHex16(minorVersion);
        //包头
        buffer.append(head);
        //流水号
        buffer.append(serial);
        //地址
        buffer.append(site);
        //业务类型
        buffer.append(type);
        //命令
        buffer.append(playVoice);
        //指定长度的高位
        buffer.append(tall);
        //指定长度的低位
        buffer.append(low);
        buffer.append(formatted);
        //除去头尾信息用CRC-16/MODBUS算法得到校验字符串
        byte[] bytes1 = hexToByteArray(serial + site + type + playVoice+tall + low   + formatted+"0000");
        String crcData = getCRCData(bytes1);
        buffer.append(crcData);
        buffer.append(stopCode);
        return hexToByteArray(buffer.toString());
    }

    public static byte[] ledGreenToRed(String time) throws Exception {
//        if (time == null || time == "") {
//            return null;
//        }
//        StringBuilder buffer = new StringBuilder();
//        StringBuilder formatted = new StringBuilder();
//        //根据，拆解播报类容
//        String[] split = time.split(",");
//        for (String spl : split) {
//            byte[] bytes = spl.getBytes("GBK");
//            formatted.append("".equals(nType.getTypeCode(spl).getValue()) ?  bytes2HexString(bytes): nType.getTypeCode(spl).getValue());
//        }
//        //获取字节长度
//        int length = formatted.length() / 2;
//        //高位字节
//        int mainVersion = (length & 0xFF00) >> 8;
//        //低位
//        int minorVersion = length & 0xFF;
//        String tall = numToHex16(mainVersion);
//        String low = numToHex16(minorVersion);
//        //包头
//        buffer.append(head);
//        //流水号
//        buffer.append(serial);
//        //地址
//        buffer.append(site);
//        //业务类型
//        buffer.append(type);
//        //命令
//        buffer.append("13");
//        //指定长度的高位
//        buffer.append(tall);
//        //指定长度的低位
//        buffer.append(low);
//        buffer.append(formatted);
//        //除去头尾信息用CRC-16/MODBUS算法得到校验字符串
//        byte[] bytes1 = hexToByteArray(serial + site + type + "13"+tall + low   + formatted+"0000");
//        String crc = getCRC(bytes1);
//        if(JddConstant.IntNumber.INT_THREE==crc.length()){
//            crc="0"+crc;
//        }
//        buffer.append(crc);
//        buffer.append(stopCode);
//        return hexToByteArray(buffer.toString());
        if (time == null || time == "") {
            return null;
        }
        StringBuilder buffer = new StringBuilder();
        //包头
        buffer.append(head);
        //流水号
        buffer.append(serial);
        //地址
        buffer.append(site);
        //业务类型
        buffer.append(type);
        //命令
        buffer.append("13");
        //指定长度的高位
        buffer.append("00");
        //指定长度的低位
        buffer.append("01");
        buffer.append(time);
//        buffer.append(serial + site + type + "1300010F");
        //除去头尾信息用CRC-16/MODBUS算法得到校验字符串
        byte[] bytes1 = hexToByteArray(serial + site + type + "130001" + time + "0000");
        String crcData = getCRCData(bytes1);
        buffer.append(crcData);
         buffer.append(stopCode);

        return hexToByteArray(buffer.toString());

    }
    /**
     * int 转16进制字符串
     *
     * @param b
     * @return
     */
    public static String numToHex16(int b) {
        return String.format("%02x", b).toUpperCase();
    }

    /**
     * byte数组转16进制字节字符串
     *
     * @param data
     * @return
     */
    public static String bytes2HexString(byte[] data) {
        if (data == null) {
            throw new NullPointerException();
        }

        StringBuilder buffer = new StringBuilder();
        for (byte b : data) {
            String hex = Integer.toHexString(b & 0xff);
            if (hex.length() == 1) {
                buffer.append('0');
            }
            buffer.append(hex);
        }
        return buffer.toString().toUpperCase();
    }





    /**
     * hex字符串转byte数组
     *
     * @param inHex 待转换的Hex字符串
     * @return 转换后的byte数组结果
     */
    public static byte[] hexToByteArray(String inHex) {
        int hexlen = inHex.length();
        byte[] result;
        if (hexlen % 2 == 1) {
            //奇数
            hexlen++;
            result = new byte[(hexlen / 2)];
            inHex = "0" + inHex;
        } else {
            //偶数
            result = new byte[(hexlen / 2)];
        }
        int j = 0;
        for (int i = 0; i < hexlen; i += 2) {
            result[j] = hexToByte(inHex.substring(i, i + 2));
            j++;
        }
        return result;
    }
    //获取校验码
    private static String getCRCData(byte[] b){
        byte[] checkOut = CrcUtil.getCRC(b);
        byte checkByte1 = checkOut[0];
        byte checkByte2 = checkOut[1];
        return getBytestr(checkByte1)+getBytestr(checkByte2);
    }

    private static String getBytestr(byte b){
        return ByteUtils.bytes2HexStr(b);
    }


    /**
     * Hex字符串转byte
     *
     * @param inHex 待转换的Hex字符串
     * @return 转换后的byte
     */
    public static byte hexToByte(String inHex) {
        return (byte) Integer.parseInt(inHex, 16);
    }
    public enum nType {
        NULL("空",""),
        HXGL("欢迎光临", "01"),
        YLPA("一路平安", "02"),
        QDDRGQR("请等待人工确认","03"),
        QJF("请缴费", "11"),
        QDD("请等待", "1E"),
        RGQR("人工确认", "1F"),
        GLC("管理处", "72"),
        YGQ("已过期", "0F"),
        CC("此车", "13"),
        RC("入场", "5C"),
        JL("记录", "16"),
        WX("无效", "10"),
        JZTX("禁止通行", "2B"),
        CWYM("车位已满", "0A"),
        CCHMD("此车黑名单", "09"),
        GSD("该时段", "64"),
        BYXJR("不允许进入", "65"),
        YLSF("一路顺风", "5F"),
        TC("停车", "2E"),
        JE("金额", "2D"),
        SY("剩余", "3D"),
        CCYGQ("此车已过期", "08"),
        ALSCSF("按临时车计费","7F08"),


        LSC("临时车", "21"),
        CZC("储值车", "23"),
        YZC("月租车", "22"),
        MFC("免费车", "24"),
        WPC("无牌车", "7F19"),

        XIAOSHI("小时", "2F");

        private final String key;
        private final String value;

        private nType(String key, String value) {
            this.key = key;
            this.value = value;
        }

        public String getKey() {
            return key;
        }

        public String getValue() {
            return value;
        }

        public static SmallFangKongLedUtils.nType getTypeCode(String key) {
            //stream使用findAny取得是第一个值
            //parallelStream使用findAny是随机
            return Arrays.asList(SmallFangKongLedUtils.nType.values()).stream()
                    .filter(alarmGrade -> alarmGrade.getKey().equals(key))
                    .findAny().orElse(nType.NULL);
        }
    }


}
